//
//  RBToast.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/6.
//

#import "RBToast.h"

//Toast默认停留时间
#define RBToastDispalyDuration 1.5f
//Toast到顶端/底端默认距离
#define RBToastSpace 100.0f
//Toast背景颜色
#define RBToastBackgroundColor [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75]
@interface RBToast (){
    UIButton *_contentView;
    CGFloat  _duration;
}
@end

@implementation RBToast

- (id)initWithText:(NSString *)text{
    if (self = [super init]) {
        UIFont *font = RBPingFangBlodFont(16);
        NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
        CGRect rect=[text boundingRectWithSize:CGSizeMake(250,CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,rect.size.width + 40, rect.size.height+ 20)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor whiteColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = font;
        textLabel.text = text;
        textLabel.numberOfLines = 0;
        
        _contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
        _contentView.layer.cornerRadius = 20.0f;
        _contentView.backgroundColor = RBToastBackgroundColor;
        [_contentView addSubview:textLabel];
        _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown];
        _contentView.alpha = 0.0f;
        _duration = RBToastDispalyDuration;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
    }
    
    return self;
}

- (id)initWithText:(NSString *)text andLeftImage:(NSString *)lmageNameStr{
    if (self = [super init]) {

        UIFont *font = [UIFont boldSystemFontOfSize:15];
        NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
        CGRect rect=[text boundingRectWithSize:CGSizeMake(250,CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
        
        UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 26-12, 24, 24)];
        leftImage.image = [UIImage imageNamed:lmageNameStr];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImage.frame)+15, 0,rect.size.width + 40, 52)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = RBColor(51, 51, 51);
        textLabel.textAlignment = NSTextAlignmentLeft;
        textLabel.font = font;
        textLabel.text = text;
        textLabel.numberOfLines = 0;
        
        _contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,RBScreenWidth-30, 52)];
        _contentView.backgroundColor = RBColor(255, 255, 255);
        _contentView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:85/255.0 blue:255/255.0 alpha:0.2].CGColor;
        _contentView.layer.shadowOffset = CGSizeMake(0,3);
        _contentView.layer.shadowOpacity = 1;
        _contentView.layer.shadowRadius = 10;
        _contentView.layer.cornerRadius = 5;
        [_contentView addSubview:leftImage];
        [_contentView addSubview:textLabel];
        _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown];
        _contentView.alpha = 0.0f;
        _duration = RBToastDispalyDuration;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
    }
    
    return self;
}

- (void)deviceOrientationDidChanged:(NSNotification *)notify{
    [self hideAnimation];
}

-(void)dismissToast{
    [_contentView removeFromSuperview];
}

-(void)toastTaped:(UIButton *)sender{
    [self hideAnimation];
}

- (void)setDuration:(CGFloat)duration{
    _duration = duration;
}

-(void)showAnimation{
    [UIView beginAnimations:@"show" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    _contentView.alpha = 1.0f;
    [UIView commitAnimations];
}

-(void)hideAnimation{
    [UIView beginAnimations:@"hide" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(dismissToast)];
    [UIView setAnimationDuration:0.3];
    _contentView.alpha = 0.0f;
    [UIView commitAnimations];
}

- (void)show{
     UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _contentView.center = window.center;
    [window  addSubview:_contentView];
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:_duration];
}

- (void)showFromTopOffset:(CGFloat)top{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _contentView.center = CGPointMake(window.center.x, top + _contentView.frame.size.height/2);
    [window  addSubview:_contentView];
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:_duration];
}

- (void)showFromBottomOffset:(CGFloat)bottom{
   UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom + _contentView.frame.size.height/2));
    [window  addSubview:_contentView];
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:_duration];
}
+ (void)rb_showCenterWithText:(NSString *)text{
    [RBToast rb_showCenterWithText:text duration:RBToastDispalyDuration];
}
+ (void)rb_showCenterWithText:(NSString *)text duration:(CGFloat)duration{
    RBToast *toast = [[RBToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast show];
}
+ (void)rb_showTopWithText:(NSString *)text{
    [RBToast rb_showTopWithText:text  topOffset:RBToastSpace duration:RBToastDispalyDuration];
}
+ (void)rb_showTopWithText:(NSString *)text duration:(CGFloat)duration{
     [RBToast rb_showTopWithText:text  topOffset:RBToastSpace duration:duration];
}
+ (void)rb_showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset{
    [RBToast rb_showTopWithText:text  topOffset:topOffset duration:RBToastDispalyDuration];
}
+ (void)rb_showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration{
    RBToast *toast = [[RBToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast showFromTopOffset:topOffset];
}
+ (void)rb_showTopWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration andLeftImage:(NSString *)lmageNameStr{
    RBToast *toast = [[RBToast alloc] initWithText:text andLeftImage:lmageNameStr];
    [toast setDuration:duration];
    [toast showFromTopOffset:topOffset];
}
+ (void)rb_showBottomWithText:(NSString *)text{
    [RBToast rb_showBottomWithText:text  bottomOffset:RBToastSpace duration:RBToastDispalyDuration];
}
+ (void)rb_showBottomWithText:(NSString *)text duration:(CGFloat)duration{
      [RBToast rb_showBottomWithText:text  bottomOffset:RBToastSpace duration:duration];
}
+ (void)rb_showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset{
    [RBToast rb_showBottomWithText:text  bottomOffset:bottomOffset duration:RBToastDispalyDuration];
}
+ (void)rb_showBottomWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration{
    RBToast *toast = [[RBToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast showFromBottomOffset:bottomOffset];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}
@end
